//
//  WXVideoHomeViewController.m
//  NewGuiYang
//
//  Created by 罗江 on 2018/5/25.
//  Copyright © 2018年 luojiang. All rights reserved.
//

#import "WXVideoHomeViewController.h"
#import "WXVideoHomeCollectionView.h"
#import <WXCloudModuleBaseView/WXNavigationBaseView.h>
#import <WXCloudModuleCategories/UIView+UIStyle.h>
#import <WXCloudModuleBaseConfig/WXSystemBaseDefine.h>
#import <WXCloudModuleCategories/UIView+LayoutMethods.h>
#import <WXXHNInternetSupervise/WXInternetSuperviseViewConfigManager.h>
#import "WXVideoHomeViewModel.h"
#import "WXPlayBackVideoModel.h"
#import "WXCloudNewsListVideoViewModel.h"
#import "WXCloudNewsListVideoViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <WXCloudModuleCacheManager/WXBaseInteractiveModel.h>
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleNewsDetailActions.h>
#import <WXMediator+WXMediatorModuleNoNetWorkActions.h>
#import <WXCloudModuleNetworking/WXNoNetWorkView.h>
#import <WXCloudModuleNetworking/WXAppContext.h>
#import <WXCloudModuleNetworking/UIView+NoNetWorkView.h>

@interface WXVideoHomeViewController ()<WXNoNetWorkViewDelegate>
@property (nonatomic, strong) WXNavigationBaseView *titleView;

@property (nonatomic, strong) WXVideoHomeCollectionView *videoHomeCollectionView;

@property (nonatomic, strong) UIView *lineView;

/** ViewModel */
@property (nonatomic, strong) WXVideoHomeViewModel *videoHomeViewModel;

@property (nonatomic, copy) NSArray *liveVideoHomeModelList;

@property (nonatomic, copy) NSArray *playBackVideoHomeModelList;

@property (nonatomic, strong) WXNoNetWorkView *noNetWorkingView;
@end

@implementation WXVideoHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initialize];
    /** 添加Reactive监听 */
    [self addReactiveObserve];
    self.view.backgroundColor=[UIColor whiteColor];

    // Do any additional setup after loading the view.
}

#pragma mark -自定义私有方法
/** 初始化参数 */
- (void)initialize
{
    _videoHomeViewModel=[WXVideoHomeViewModel new];
    [_videoHomeViewModel requestVideoHomeModelList];

    [self.view addSubview:self.videoHomeCollectionView];
    [self.view addSubview: self.titleView];
    
    if ([WXAppContext sharedInstance].isReachable) {
        if (self.noNetWorkingView !=nil) {
            [self.noNetWorkingView removeFromSuperview];
        }
    } else {
        self.noNetWorkingView = [self.videoHomeCollectionView addNoNetWorkingView];
        self.noNetWorkingView.delegate=self;
    }

    
}
/** RAC监听方法 */
- (void)addReactiveObserve
{
    @weakify(self)
    [RACObserve(_videoHomeViewModel, isRequestVideoDataSuccess) subscribeNext:^(id x) {
        @strongify(self)
        if (self.videoHomeViewModel.isRequestVideoDataSuccess) {
            self.liveVideoHomeModelList = self.videoHomeViewModel.liveVideoHomeModelList;
            self.playBackVideoHomeModelList= self.videoHomeViewModel.playBackVideoHomeModelList;
            self.videoHomeCollectionView.videoHomeList=self.liveVideoHomeModelList;
            self.videoHomeCollectionView.videoPlayBackList=self.playBackVideoHomeModelList;
            [self.videoHomeCollectionView reloadData];
           // [self.playBackCollectionView reloadData];
            
        }
        else{
            
        }
    }];
    
    [[self.videoHomeCollectionView rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:)] subscribeNext:^(NSArray *params) {
        @strongify(self)
        NSIndexPath *indexPath = params[1];
        if (indexPath.section==0) {
            WXBaseInteractiveModel *model = self.liveVideoHomeModelList[indexPath.item];
            if (model!=nil) {
                UIViewController *targerVC = [[WXMediator sharedInstance] WXMediator_showNewsWithModelAction:model];
                [self.navigationController pushViewController:targerVC animated:YES];
            }
        }
        else{
            WXPlayBackVideoModel*model=self.playBackVideoHomeModelList[indexPath.item];
            NSLog(@"model==%@",model);
            WXCloudNewsListVideoViewModel *viewModel = [WXCloudNewsListVideoViewModel new];
            viewModel.chanelTitle = model.ClassCN;
            viewModel.classID = model.ID;
            [viewModel tableViewHeaderRereshing];
            WXCloudNewsListVideoViewController *videoListVC = [[WXCloudNewsListVideoViewController alloc] initWithViewModel:viewModel];
             [self.navigationController pushViewController:videoListVC animated:YES];
        }
    }];
}

#pragma mark WXNoNetWorkViewDelegate代理方法
/** 顶部按钮代理 */
- (void)noNetWorkViewdidClickNoNetWorkTopButton
{
    UIViewController *targetVC= [[WXMediator sharedInstance] WXMediator_getNoNetWorkAction];
    [self.navigationController pushViewController:targetVC animated:YES];
}
- (void)noNetWorkViewdidClickNoNetWorkFullScreenButton{
    if ([WXAppContext sharedInstance].isReachable) {
        if (self.noNetWorkingView !=nil) {
            [self.noNetWorkingView removeFromSuperview];
        }
        [_videoHomeViewModel requestVideoHomeModelList];
    }else{
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (WXNavigationBaseView *)titleView
{
    if (_titleView == nil) {
        _titleView = [[WXNavigationBaseView alloc]initWithTitle:@"视频"];
        _titleView.titleLabel.textColor = [WXInternetSuperviseViewConfigManager sharedInstance].TitleLabelColor;
        _titleView.backgroundColor = [WXInternetSuperviseViewConfigManager sharedInstance].TitleBackgroundColor;
        [_titleView addNormalShadow];
        
    }
    return _titleView;
}
-(WXVideoHomeCollectionView *)videoHomeCollectionView{
    if (_videoHomeCollectionView==nil) {
        _videoHomeCollectionView = [[WXVideoHomeCollectionView alloc]initWithFrame:CGRectMake(0, 64, WXScreenWidth, WXScreenHeight-64-50)];
    }
    return _videoHomeCollectionView;
}

@end
